﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using System.Text.RegularExpressions;

namespace CECB
{
    /// <summary>
    /// Interaction logic for Members_Window.xaml
    /// </summary>
    public partial class Members_Window : Window
    {
        public Members_Window()
        {
            InitializeComponent();
            comboBox_Specilised.ItemsSource = new Dss.Methods.MemberInfo().getSpecializatioInformations();
            comboBox_ModifySpecilised.ItemsSource = new Dss.Methods.MemberInfo().getSpecializatioInformations();
            MemberAutocompleteTextBox.ItemsSource = new Dss.Methods.MemberInfo().getAllMembers();
            autoCompleteBox_SearchMember.ItemsSource = new Dss.Methods.MemberInfo().getAllMembers();


        }

        private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);

        }


        private void Email_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {

        }



        private void button_Save_Cancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {


            if (textBox_FirstName.Text.Length == 0)
            {
                MessageBox.Show("Enter First Name", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            else if (textBox_Address.Text.Length == 0)
            {
                MessageBox.Show("Enter Address", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (comboBox_Designation.SelectedIndex == -1)
            {
                MessageBox.Show("select Designation", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else if (comboBox_Specilised.SelectedIndex == -1)
            {
                MessageBox.Show("select Specilization", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else if (textBox_ContactNo1.Text.Length == 0 && textBox_ContactNo2.Text.Length == 0 && textBox_ContactNo3.Text.Length == 0)
            {

                MessageBox.Show("Enter Atleast One Contact No", "Message", MessageBoxButton.OK, MessageBoxImage.Error);


            }
            else if (textBox_ContactNo1.Text.Length != 0 && textBox_ContactNo1.Text.Length != 10)
            {

                MessageBox.Show("Enter valid Contact No");

            }
            else if (textBox_ContactNo2.Text.Length != 0 && textBox_ContactNo2.Text.Length != 10)
            {

                MessageBox.Show("Enter valid Contact No");

            }
            else if (textBox_ContactNo3.Text.Length != 0 && textBox_ContactNo3.Text.Length != 10)
            {

                MessageBox.Show("Enter valid Contact No");

            }

            else if (textBox_Email1.Text.Length == 0 && textBox_Email2.Text.Length == 0)
            {
                MessageBox.Show("Enter Atleast One Email Address", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (textBox_Email2.Text.Length != 0 && (!Regex.IsMatch(textBox_Email2.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")))
            {
                MessageBox.Show("Enter valid Email Address", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (textBox_Email1.Text.Length != 0 && (!Regex.IsMatch(textBox_Email1.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")))
            {
                MessageBox.Show("Enter valid Email Address", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                List<String> contactNo = new List<string>();
                if (textBox_ContactNo1.Text.Trim() != "")
                    contactNo.Add(textBox_ContactNo1.Text.Trim());
                if (textBox_ContactNo2.Text.Trim() != "")
                    contactNo.Add(textBox_ContactNo2.Text.Trim());
                if (textBox_ContactNo3.Text.Trim() != "")
                    contactNo.Add(textBox_ContactNo3.Text.Trim());

                List<String> employEmail = new List<string>();
                if (textBox_Email1.Text.ToString() != "")
                    employEmail.Add(textBox_Email1.Text.ToString());
                if (textBox_Email2.Text.ToString() != "")
                    employEmail.Add(textBox_Email2.Text.ToString());



                Dss.Methods.MemberInfo memins = new Dss.Methods.MemberInfo();
                if (comboBox_Designation.Text == "Consultent")
                {
                    if (memins.InsertConsultantBasicInfo(textBox_FirstName.Text.Trim(), textBox_Address.Text.Trim(), comboBox_Designation.Text, comboBox_Specilised.Text.Trim(), contactNo, employEmail, comboBox_Specilised.Text.Trim()))
                    {
                        MessageBox.Show("Informations Added Successfully!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                        comboBox_Specilised.ItemsSource = new Dss.Methods.MemberInfo().getSpecializatioInformations();
                        comboBox_ModifySpecilised.ItemsSource = new Dss.Methods.MemberInfo().getSpecializatioInformations();
                        MemberAutocompleteTextBox.ItemsSource = new Dss.Methods.MemberInfo().getAllMembers();
                        autoCompleteBox_SearchMember.ItemsSource = new Dss.Methods.MemberInfo().getAllMembers();

                    }
                }
                else if (comboBox_Designation.Text == "Team Leader")
                {
                    if (memins.insertTeamLeadeaderInformations(textBox_FirstName.Text.Trim(), textBox_Address.Text.Trim(), comboBox_Designation.Text, comboBox_Specilised.Text.Trim(), contactNo, employEmail, textBox_username.Text.Trim(), textBox_Password.Password.Trim(), comboBox_Specilised.Text.Trim()))
                    {
                        MessageBox.Show("Informations Added Successfully!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                        comboBox_Specilised.ItemsSource = new Dss.Methods.MemberInfo().getSpecializatioInformations();
                        comboBox_ModifySpecilised.ItemsSource = new Dss.Methods.MemberInfo().getSpecializatioInformations();
                        MemberAutocompleteTextBox.ItemsSource = new Dss.Methods.MemberInfo().getAllMembers();
                        autoCompleteBox_SearchMember.ItemsSource = new Dss.Methods.MemberInfo().getAllMembers();

                    }
                }
            }

        }

        public void clear()
        {
            textBox_FirstName.Clear();
            textBox_ContactNo1.Clear();
            textBox_ContactNo2.Clear();
            textBox_ContactNo3.Clear();
            textBox_Email1.Clear();
            textBox_Email2.Clear();
            comboBox_Designation.SelectedIndex = -1;
            comboBox_Specilised.SelectedIndex = -1;
            textBox_ContactNo1.Clear();
            textBox_Address.Clear();
            textBox_username.Clear();
            textBox_Password.Clear();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {
            clear();

        }

        private void textBox_Designation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox_Designation.SelectedIndex == 1)
                wrapPanelUserLogin.Visibility = System.Windows.Visibility.Collapsed;
            else
                wrapPanelUserLogin.Visibility = System.Windows.Visibility.Visible;
        }

        private void listBoxContacts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBoxContacts.Items.Count > 0)
            {
                try
                {
                    MemberContacts memc = (MemberContacts)listBoxContacts.SelectedItem;
                    textBoxUpdateContact.Text = memc.mem_contactNo.ToString();
                    _oldContactNo = memc.mem_contactNo.ToString();
                    _memID = memc.mem_reference.ToString();
                    label_Change.Content = "Change Contact No";
                }
                catch (Exception)
                {


                }
            }
        }

        private void ReferenceTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Dss.Methods.MemberInfo memInfo = new Dss.Methods.MemberInfo();

            listBoxContacts.ItemsSource = new Dss.Methods.MemberInfo().getMemberContatcs(MemberAutocompleteTextBox.Text);
            listBoxEmail.ItemsSource = new Dss.Methods.MemberInfo().getMemberEmail(MemberAutocompleteTextBox.Text);
            label_memName.Content = new Dss.Methods.MemberInfo().getMemberName(MemberAutocompleteTextBox.Text);
        }




        String _oldContactNo = "";
        String _memID = "";
        String _oldEmail = "";
        String _memIDEmail = "";


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxUpdateContact.Text == "")
            {
                MessageBox.Show("Nothing selected to modify", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                if (label_Change.Content == "Change Contact No")
                {
                    if (textBoxUpdateContact.Text.Length != 0 && textBoxUpdateContact.Text.Length != 10)
                    {
                        MessageBox.Show("Enter a valid contact No ", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }

                    else
                    {
                        if (new Dss.Methods.MemberInfo().updateMemberContact(textBoxUpdateContact.Text.Trim(), _memID, _oldContactNo))
                        {
                            MessageBox.Show("Contact Updated Successfully!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            textBoxUpdateContact.Clear();
                            listBoxContacts.ItemsSource = new Dss.Methods.MemberInfo().getMemberContatcs(MemberAutocompleteTextBox.Text);



                        }
                    }
                }
                else
                {
                    if (textBoxUpdateContact.Text.Length != 0 && (!Regex.IsMatch(textBoxUpdateContact.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")))
                    {
                        MessageBox.Show("Enter a valid Email Address");
                    }
                    else
                    {
                        if (new Dss.Methods.MemberInfo().updateMemberEmails(textBoxUpdateContact.Text.Trim(), _memIDEmail, _oldEmail))
                        {
                            MessageBox.Show("Email Address Updated Successfully!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            textBoxUpdateContact.Clear();
                            listBoxContacts.ItemsSource = new Dss.Methods.MemberInfo().getMemberEmail(MemberAutocompleteTextBox.Text);
                        }
                    }
                }
            }
            listBoxContacts.ItemsSource = new Dss.Methods.MemberInfo().getMemberContatcs(MemberAutocompleteTextBox.Text);
            listBoxEmail.ItemsSource = new Dss.Methods.MemberInfo().getMemberEmail(MemberAutocompleteTextBox.Text);
        }


        private void listBoxEmail_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBoxEmail.Items.Count > 0)
            {
                try
                {
                    MemberEmails memEmail = (MemberEmails)listBoxEmail.SelectedItem;
                    textBoxUpdateContact.Text = memEmail.mem_email.ToString();
                    _oldEmail = memEmail.mem_email.ToString();
                    _memIDEmail = memEmail.mem_reference.ToString();
                    label_Change.Content = "Change Email Address";

                }
                catch (Exception)
                {


                }
            }
        }

        private void textBox_ContactNo3_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);


        }

        private void textBox_ContactNo3_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBoxUpdateContact_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (label_Change.Content == "Change Contact No")
            {
                Regex regex = new Regex("[^0-9]+");
                e.Handled = regex.IsMatch(e.Text);
            }
        }

        private void Label_SourceUpdated(object sender, DataTransferEventArgs e)
        {

        }

        private void autoCompleteBox_SearchMember_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            new Dss.Methods.MemberInfo().getMemberDetails(autoCompleteBox_SearchMember.Text);
            new Dss.Methods.MemberInfo().getMemberLoginDetails(autoCompleteBox_SearchMember.Text);

            try
            {

                List<Members> aa = new List<Members>();
                List<MemberLogin> bb = new List<MemberLogin>();
                bb = new Dss.Methods.MemberInfo().getMemberLoginDetails(autoCompleteBox_SearchMember.Text);
                aa = new Dss.Methods.MemberInfo().getMemberDetails(autoCompleteBox_SearchMember.Text);
                textBox_ModifyAddress.Text = aa[0].mem_address;
                textBox_ModifyFirstName.Text = aa[0].mem_name;
                textBox_Modifyusername.Text = bb[0].login_username;
                comboBox_ModifySpecilised.Text = aa[0].mem_areaOfSpec;
                comboBox_ModifyDesignation.Text = aa[0].mem_designation;




            }
            catch (Exception)
            {


            }
        }

        private void updateButton_Click(object sender, RoutedEventArgs e)
        {
            Dss.Methods.MemberInfo aa = new Dss.Methods.MemberInfo();
            if (aa.updateMemberDetails(autoCompleteBox_SearchMember.Text, textBox_ModifyAddress.Text, textBox_ModifyFirstName.Text, comboBox_ModifyDesignation.Text, comboBox_ModifySpecilised.Text))
            {
                MessageBox.Show("Successfully Updated");

                comboBox_Specilised.ItemsSource = new Dss.Methods.MemberInfo().getSpecializatioInformations();
                comboBox_ModifySpecilised.ItemsSource = new Dss.Methods.MemberInfo().getSpecializatioInformations();
                MemberAutocompleteTextBox.ItemsSource = new Dss.Methods.MemberInfo().getAllMembers();
                autoCompleteBox_SearchMember.ItemsSource = new Dss.Methods.MemberInfo().getAllMembers();


            }
            else
            {
                MessageBox.Show("Selected nothing to modify", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            clearAfterUpdate();
            comboBox_ModifySpecilised.ItemsSource = new Dss.Methods.MemberInfo().getSpecializatioInformations();


        }

        public void clearAfterUpdate()
        {

            textBox_ModifyFirstName.Clear();
            textBox_ModifyAddress.Clear();

            textBox_Modifyusername.Clear();
            comboBox_ModifyDesignation.SelectedIndex = -1;
            comboBox_ModifySpecilised.ItemsSource = null;

            if (comboBox_ModifySpecilised.Items.Count != 0)
            {
                comboBox_ModifySpecilised.Items.Clear();
            }


        }

        private void textBox_Modifyusername_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBox_Modifyusername_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }

        private void textBox_Modifyusername_TextInput(object sender, TextCompositionEventArgs e)
        {

        }

        private void textBox_Modifyusername_IsHitTestVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }

        private void comboBox_Specilised_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void textBoxUpdateContact_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }


    }
}

   